#!/bin/bash

set +e

if [ -z "$1"   ]
then
  echo "缺少 IP..."
  exit 1;
fi


# if [ -z "$2"   ]
# then
#   echo "请设置组别"
#   exit 1;
# fi

# if [ -z "$3"   ]
# then
#   echo "输入服务类型: ap p1 p2"
#   exit 1;
# fi


if [ $2 == 'ap' ];then
  echo "创建 AP 服务"
  echo ""
  sleep 1
  echo "暂时禁止 AP 服务创建"
  exit 1;
elif [ $2 == 'p1' ];then
  echo ""
  sleep 1
elif [ $2 == 'pcto1' ];then
  echo ""
  sleep 1
elif [ $2 == 'pcto2' ];then
  echo ""
  sleep 1
else
  echo "输入服务类型: ap p1 pcto1 pcto2"
  exit 1;
fi

TAGS=0
NUMB=$(($TAGS-1))
BASE_PORT=2346
CONF_PORT=$(($TAGS+2346))


curPath=$(dirname $(readlink -f "$0"))
PASSWD=$(cat $curPath/../.env)

IFUSER=ceshi
i=$1
IPPrefix="192.168.8."
CONF_IP=$1

# LOTUS_TOKEN='/usr/local/bin/lotus auth api-info --perm admin'
# MINER_TOKEN=$4
MINER_TOKEN=$(/usr/local/bin/lotus-miner auth api-info --perm admin)

# MINER_TOKEN=${MINER_TOKEN//0.0.0.0/10.0.1.200}

echo "start worker to $IPPrefix$1 - $2  ===> $2"





# sudo vim /lib/systemd/system/worker-ap.service




UNSEAL=false
ADDPIECE=false
PRECOMMIT1=false
PRECOMMIT2=true
COMMIT=false
NO_SWAP=false
FETCHLIMIT=7

AP=4
PC1=14  # 22台
# PC1=10  # 9台
PC2=1
C1=1
C2=1
FIN=1
GROUP=$1
# GROUP=`expr $1 + 100`;
COMMD=""
CONF_NAME="/lib/systemd/system/worker-$2.service"
echo $CONF_NAME

ABILITY="AP:$AP,PC1:$PC1,PC2:$PC2,C1:$C1,C2:$C2,FIN:$FIN,GET:${FETCHLIMIT}"


echo "start worker to $IPPrefix$1 -  ===> $GROUP"


# WORKER="/var/tmp"
WORKER="/home/${IFUSER}/.lotusworker"

WORKER_STORAGE="/home/${IFUSER}/.storage"
WORKER_REPO_AP="/home/${IFUSER}/.lotusworker/.worker_ap"
WORKER_REPO_P1="/home/${IFUSER}/.lotusworker/.worker"
WORKER_REPO_PC1="/home/${IFUSER}/.lotusworker/.worker_p2_c2_1"
WORKER_REPO_PC2="/home/${IFUSER}/.lotusworker/.worker_p2_c2_2"

if [ $2 == 'ap' ];then
    COMMD="cat > $CONF_NAME <<EOF

[Unit]
Description=Worker AP
# Documentation=https://docs.filecoin.io
After=network.target

[Service]
User=${IFUSER}
Group=${IFUSER}

ExecStart=/usr/local/bin/lotus-worker --worker-repo=${WORKER_REPO_AP} run \\
            --ability=AP:3,PC1:12,PC2:1,C1:1,C2:1 \\
            --listen=${IPPrefix}${CONF_IP}:$(($BASE_PORT)) \\
            --no-local-storage=false \\
            --group=${GROUP} \\
            --unseal=false \\
            --addpiece=true \\
            --precommit1=false \\
            --precommit2=false \\
            --commit=false \\
            --no-swap=true \\
            --parallel-fetch-limit=${FETCHLIMIT}
Environment=FIL_PROOFS_USE_MULTICORE_SDR=1
Environment=FIL_PROOFS_MULTICORE_SDR_PRODUCERS=1
Environment=FIL_PROOFS_MAXIMIZE_CACHING=1
Environment=FIL_PROOFS_VERIFY_CACHE=1
Environment=BELLMAN_NO_GPU=true
Environment=FIL_PROOFS_SDR_PARENTS_CACHE_SIZE=1073741824
Environment=${MINER_TOKEN}
Environment=RUST_LOG=info
Environment=RUST_BACKTRACE=full
Environment=RUSTFLAGS=\"-C target-cpu=native -g\"
Environment=FFI_BUILD_FROM_SOURCE=1
Environment=GOLOG_FILE=/home/${IFUSER}/.lotuslogs/ap.log

Nice=5
LimitNOFILE=1048576

CPUAffinity=0-7
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target

EOF"

elif [ $2 == 'p1' ];then
    COMMD="cat > $CONF_NAME <<EOF 
[Unit]
Description=Worker P1
# Documentation=https://docs.filecoin.io
After=network.target

[Service]
User=${IFUSER}
Group=${IFUSER}

ExecStart=/usr/local/bin/lotus-worker --worker-repo=${WORKER_REPO_P1} run \
            --ability=${ABILITY} \
            --listen=${IPPrefix}${CONF_IP}:$(($BASE_PORT+1)) \
            --no-local-storage=false \
            --group=${GROUP} \
            --unseal=false \
            --addpiece=true \
            --precommit1=true \
            --precommit2=false \
            --commit=false \
            --no-swap=true \
            --parallel-fetch-limit=${FETCHLIMIT} 

Environment=RUST_LOG=info
Environment=RUST_BACKTRACE=full
Environment=RUSTFLAGS=\"-C target-cpu=native -g\"
Environment=FFI_BUILD_FROM_SOURCE=1

Environment=${MINER_TOKEN}
# Environment=BELLMAN_NO_GPU=true
Environment=FIL_PROOFS_USE_MULTICORE_SDR=1
Environment=FIL_PROOFS_MULTICORE_SDR_PRODUCERS=1
Environment=FIL_PROOFS_SDR_PARENTS_CACHE_SIZE=1073741824
Environment=FIL_PROOFS_MAXIMIZE_CACHING=1
Environment=FIL_PROOFS_VERIFY_CACHE=1

Environment=FIL_PROOFS_PARENT_CACHE=$WORKER_STORAGE/filecoin-parents


Environment=GOLOG_FILE=/home/${IFUSER}/.lotuslogs/all.log

Nice=5
LimitNOFILE=1048576
CPUAffinity=0-15,16-31
#Restart=always
#RestartSec=30

[Install]
WantedBy=multi-user.target



EOF"

elif [ $2 == 'pcto1' ];then
    COMMD="cat > $CONF_NAME <<EOF 

[Unit]
Description=Worker P2C2_1
# Documentation=https://docs.filecoin.io
After=network.target

[Service]
User=${IFUSER}
Group=${IFUSER}

ExecStart=/usr/local/bin/lotus-worker --worker-repo=${WORKER_REPO_PC1} run \
            --ability=${ABILITY} \
            --listen=${IPPrefix}${CONF_IP}:$(($BASE_PORT+2)) \
            --no-local-storage=false \
            --group=${GROUP} \
            --unseal=false \
            --addpiece=false \
            --precommit1=false \
            --precommit2=true \
            --commit=false \
            --no-swap=true \
            --parallel-fetch-limit=${FETCHLIMIT} 

Environment=RUST_LOG=debug
Environment=RUST_BACKTRACE=full
Environment=RUSTFLAGS=\"-C target-cpu=native -g\"
Environment=FFI_BUILD_FROM_SOURCE=1

Environment=FIL_PROOFS_USE_MULTICORE_SDR=1
Environment=FIL_PROOFS_MULTICORE_SDR_PRODUCERS=1
Environment=FIL_PROOFS_SDR_PARENTS_CACHE_SIZE=1073741824
Environment=FIL_PROOFS_MAXIMIZE_CACHING=1
Environment=FIL_PROOFS_VERIFY_CACHE=1

Environment=FIL_PROOFS_USE_GPU_COLUMN_BUILDER=1
Environment=FIL_PROOFS_USE_GPU_TREE_BUILDER=1

Environment=GPU_DEVICE_ORDINAL=0
Environment=CUDA_VISIBLE_DEVICES=0
# Environment=BELLMAN_CPU_UTILIZATION=0.05

Environment=${MINER_TOKEN}
Environment=TMPDIR=$WORKER/tmp_p2_c2_1
Environment=FIL_PROOFS_PARENT_CACHE=$WORKER_STORAGE/filecoin-parents
Environment=FIL_PROOFS_PARAMETER_CACHE=$WORKER_STORAGE/filecoin-proof-parameters


Environment=GOLOG_FILE=/home/${IFUSER}/.lotuslogs/allpcto1.log

Nice=5
LimitNOFILE=1048576

CPUAffinity=32-47
# Restart=always
# RestartSec=30

[Install]
WantedBy=multi-user.target


EOF"

elif [ $2 == 'pcto2' ];then
    COMMD="cat > $CONF_NAME <<EOF 

[Unit]
Description=Worker P2C2_2
# Documentation=https://docs.filecoin.io
After=network.target

[Service]
User=${IFUSER}
Group=${IFUSER}

ExecStart=/usr/local/bin/lotus-worker --worker-repo=${WORKER_REPO_PC2} run \
            --ability=${ABILITY} \
            --listen=${IPPrefix}${CONF_IP}:$(($BASE_PORT+3)) \
            --no-local-storage=false \
            --group=${GROUP} \
            --unseal=false \
            --addpiece=false \
            --precommit1=false \
            --precommit2=true \
            --commit=false \
            --no-swap=true \
            --parallel-fetch-limit=${FETCHLIMIT} 

Environment=RUST_LOG=debug
Environment=RUST_BACKTRACE=full
Environment=RUSTFLAGS=\"-C target-cpu=native -g\"
Environment=FFI_BUILD_FROM_SOURCE=1

Environment=FIL_PROOFS_USE_MULTICORE_SDR=1
Environment=FIL_PROOFS_MULTICORE_SDR_PRODUCERS=1
Environment=FIL_PROOFS_SDR_PARENTS_CACHE_SIZE=1073741824
Environment=FIL_PROOFS_MAXIMIZE_CACHING=1
Environment=FIL_PROOFS_VERIFY_CACHE=1

Environment=FIL_PROOFS_USE_GPU_COLUMN_BUILDER=1
Environment=FIL_PROOFS_USE_GPU_TREE_BUILDER=1

Environment=GPU_DEVICE_ORDINAL=1
Environment=CUDA_VISIBLE_DEVICES=1
# Environment=BELLMAN_CPU_UTILIZATION=0.05

Environment=${MINER_TOKEN}
Environment=TMPDIR=$WORKER/tmp_p2_c2_2
Environment=FIL_PROOFS_PARENT_CACHE=$WORKER_STORAGE/filecoin-parents
Environment=FIL_PROOFS_PARAMETER_CACHE=$WORKER_STORAGE/filecoin-proof-parameters

Environment=GOLOG_FILE=/home/${IFUSER}/.lotuslogs/allpcto2.log

Nice=5
LimitNOFILE=1048576

CPUAffinity=48-63
# Restart=always
# RestartSec=30

[Install]
WantedBy=multi-user.target

EOF"

fi

# sshpass -p $PASSWD ssh -o ConnectTimeout=4 $IFUSER@$CONF_IP "cat > ~/$CONF_NAME <<EOF
sshpass -p $PASSWD ssh -o ConnectTimeout=4 -o ConnectionAttempts=5 $IFUSER@$IPPrefix$i "sudo sh -c '$COMMD'"

sshpass -p $PASSWD ssh -o ConnectTimeout=4 -o ConnectionAttempts=5 $IFUSER@$IPPrefix$i "cat $CONF_NAME && sudo systemctl daemon-reload"


# bash ./g_service_9.sh 240 8 ap

# bash ~/miner-sh/install/dev.sh 240 247  "sudo systemctl start  worker-ap"